using Autofac;
using Autofac.Configuration;
using Autofac.Extras.DynamicProxy;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Publicuse.Autofac;
using Publicuse.Interface;
using Publicuse.Service;
using System.Reflection;
using Module = Autofac.Module;

namespace Geekeradmin.Common
{
    public class CustomAutofacModule : Module
    {
        /// <summary>
        /// 加载注入服务文件
        /// </summary>
        /// <param name="containerBuilder"></param>
        protected override void Load(ContainerBuilder containerBuilder)
        {
            #region 注册拦截器和接口服务
            containerBuilder.Register(a => new IAutofacAop());
            containerBuilder.RegisterType<LogInterceptor>();
            containerBuilder.RegisterType<ExceptionInerceptor>();
            containerBuilder.RegisterType<CacheInterceptor>();
            containerBuilder.RegisterType<AutofaceInstance>().As<IAutofaceInstance>()
              .InterceptedBy(typeof(ExceptionInerceptor)).InterceptedBy(typeof(LogInterceptor)).InterceptedBy(typeof(CacheInterceptor)).EnableInterfaceInterceptors();
            #endregion

            #region 注册程序集
            Assembly interfaceAssembly = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "\\Publicuse.Interface.dll");
            Assembly serviceAssembly = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "\\Publicuse.Service.dll");
            containerBuilder.RegisterAssemblyTypes(interfaceAssembly, serviceAssembly).AsImplementedInterfaces().PropertiesAutowired(new CusotmPropertySelector());

            #endregion

            #region 注册每个控制器和抽象之间的关系
            var controllerBaseType = typeof(ControllerBase);
            containerBuilder.RegisterAssemblyTypes(typeof(Program).Assembly)
                .Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
            .PropertiesAutowired(new CusotmPropertySelector()); //支持属性注入
            #endregion

            #region Autofac 基于配置文件的服务注册
            //JSON格式配置文件
            //IConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); // 配置文件的读取器 
            // 通过JSON文件
            //IConfigurationSource autofacJsonConfigSource = new JsonConfigurationSource()
            //{
            //    Path = "Autofac.json",
            //    Optional = false,//boolean,默认就是false,可不写
            //    ReloadOnChange = false,//同上
            //};
            //configurationBuilder.Add(autofacJsonConfigSource);
            //configurationBuilder.AddJsonFile("ConfigFile/Autofac.json");
            //var module = new ConfigurationModule(configurationBuilder.Build());

            // 也可根据配置Xml配置文件来配置
            //configurationBuilder.AddXmlFile("ConfigFile/AutofacConfig.xml");
            //IConfigurationRoot root = configurationBuilder.Build();
            // 开始读取配置文件里的内容信息
            // var module = new ConfigurationModule(root);

            // 根据配置文件的内容注册服务
            //containerBuilder.RegisterModule(module);
            #endregion

        }
    }
}
